package com.hd123.rumba.util.lunar;

/** 阳历日期类,继承自定义日期 */
public class SolarDate extends MyDate {

  private static int checkMonth(int iMonth) {
    if (iMonth > 12) {
      System.out.println("Month out of range, I think you want 12 :)");
      return 12;
    } else if (iMonth < 1) {
      System.out.println("Month out of range, I think you want 1 :)");
      return 1;
    } else
      return iMonth;
  }

  private static int checkDay(int iYear, int iMonth, int iDay) {
    int iMonthDays = LunarCalendar.iGetSYearMonthDays(iYear, iMonth);

    if (iDay > iMonthDays) {
      System.out.println("Day out of range, I think you want " + iMonthDays + " :)");
      return iMonthDays;
    } else if (iDay < 1) {
      System.out.println("Day out of range, I think you want 1 :)");
      return 1;
    } else
      return iDay;
  }

  public void setDate(java.util.Date d) {
    java.util.Calendar c = java.util.Calendar.getInstance();
    c.setTime(d);
    int iYear = c.get(java.util.Calendar.YEAR);
    int iMonth = c.get(java.util.Calendar.MONTH);
    int iDay = c.get(java.util.Calendar.DATE);
    this.iYear = super.checkYear(iYear);
    this.iMonth = checkMonth(iMonth)+1;
    this.iDay = checkDay(this.iYear, this.iMonth, iDay);
  }
  
  public java.util.Date getDate() {
    java.util.Calendar c = java.util.Calendar.getInstance();
    c.clear();
    c.set(iYear, iMonth-1, iDay);
    return c.getTime();
  }

  public SolarDate(int iYear, int iMonth, int iDay) {
    super(iYear);
    this.iMonth = checkMonth(iMonth);
    this.iDay = checkDay(this.iYear, this.iMonth, iDay);
  }

  public SolarDate(int iYear, int iMonth) {
    super(iYear);
    this.iMonth = checkMonth(iMonth);
  }

  public SolarDate(int iYear) {
    super(iYear);
  }

  public SolarDate() {
    super();
  }

  public String toString() {
    return "" + this.iYear + (this.iMonth > 9 ? "-" + this.iMonth : "-0" + this.iMonth)
        + (this.iDay > 9 ? "-" + this.iDay : "-0" + this.iDay);
  }

  public WeekDay toWeek() {
    int iOffsetDays = 0;
    for (int i = 1901; i < iYear; i++) {
      if (LunarCalendar.bIsSolarLeapYear(i))
        iOffsetDays += 366;
      else
        iOffsetDays += 365;
    }

    iOffsetDays += LunarCalendar.iGetSNewYearOffsetDays(iYear, iMonth, iDay);
    return new WeekDay((iOffsetDays + 2) % 7);
  }

  public LunarDate toLunarDate() {
    int iYear, iMonth, iDay, iDate;
    LunarDate ld;
    iDate = Integer.parseInt(LunarCalendar.sCalendarSolarToLundar(this.iYear, this.iMonth, this.iDay));
    iYear = iDate / 10000;
    iMonth = iDate % 10000 / 100;
    iDay = iDate % 100;
    ld = new LunarDate(iYear, iMonth, iDay);
    return ld;
  }
}
